<?php


class Language extends CActiveRecord
{

	public function tableName()
	{
		return 'language';
	}

	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('code, locale, image, status', 'required'),
			array('sort_order, status', 'numerical', 'integerOnly'=>true),
			array('name, directory', 'length', 'max'=>32),
			array('code', 'length', 'max'=>5),
			array('locale', 'length', 'max'=>255),
			array('image, filename', 'length', 'max'=>64),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('language_id, name, code, locale, image, directory, filename, sort_order, status', 'safe', 'on'=>'search'),
		);
	}

	public function relations()
	{
		return array(
		);
	}

	public function attributeLabels()
	{
		return array(
			'language_id' => 'Language',
			'name' => 'Name',
			'code' => 'Code',
			'locale' => 'Locale',
			'image' => 'Image',
			'directory' => 'Directory',
			'filename' => 'Filename',
			'sort_order' => 'Sort Order',
			'status' => 'Status',
		);
	}

    public function getAll()
    {
        return $this->findAllByAttributes(
            array('status'=>1),
            array('order'=>'sort_order')
        );
    }

    public function getLang($code)
    {
        return $this->findAllByAttributes(array(
            'code'=>$code,
            'status'=>1,
        ));
    }

    public function getDefaultLang()
    {
        return Setting::getConfig('config_language');
    }

	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('language_id',$this->language_id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('locale',$this->locale,true);
		$criteria->compare('image',$this->image,true);
		$criteria->compare('directory',$this->directory,true);
		$criteria->compare('filename',$this->filename,true);
		$criteria->compare('sort_order',$this->sort_order);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
